package top.heue.test.preference

import androidx.preference.Preference
import com.hjq.permissions.XXPermissions
import top.heue.test.R
import top.heue.test.app.T
import top.heue.test.app.Values
import top.heue.test.util.PermissionUtil
import top.heue.utils.base.base.BasePreference
import top.heue.utils.log.L
import top.heue.utils.v

class DebugPreference : BasePreference() {
    override val name: String = "调试"
    override val resource: Int = R.xml.preference_debug

    private val getDirOfPath by v
    private val requestWritePermission by v

    override fun onClick(key: String, preference: Preference) {
        super.onClick(key, preference)
        when (key) {
            getDirOfPath -> {
                val tag = "FileDebug"
                val path = "a/b"
                L.w(tag, Values.getDirOfPath(path))
                L.w(tag, Values.getDirOfPath(path, Values.PATH_STORAGE))
                L.w(tag, Values.getDirOfPath(path, Values.PATH_PRIVATE))
                L.w(tag, Values.getDirOfPath(path, Values.PATH_EX_PRIVATE))
            }
            requestWritePermission -> {
                val pu = PermissionUtil(requireContext())
                if (pu.isGranted) {
                    T.show("权限已全部授予")
                } else {
                    XXPermissions.with(requireContext())
                        .permission(pu.permissions)
                        .request { permissions, allGranted ->
                            if (allGranted) {
                                T.show("全部申请成功")
                            }
                            L.d(permissions)
                        }
                }
            }
        }
    }
}